// 格式化工具类

// 格式化时间
function formatTime(date) {
  const now = new Date();
  const time = new Date(date);
  const diff = now - time;
  
  // 计算时间差
  const seconds = Math.floor(diff / 1000);
  const minutes = Math.floor(seconds / 60);
  const hours = Math.floor(minutes / 60);
  const days = Math.floor(hours / 24);
  
  if (days > 0) {
    return `${days}天前`;
  } else if (hours > 0) {
    return `${hours}小时前`;
  } else if (minutes > 0) {
    return `${minutes}分钟前`;
  } else {
    return '刚刚';
  }
}

// 格式化日期
function formatDate(date) {
  const d = new Date(date);
  const year = d.getFullYear();
  const month = String(d.getMonth() + 1).padStart(2, '0');
  const day = String(d.getDate()).padStart(2, '0');
  const hours = String(d.getHours()).padStart(2, '0');
  const minutes = String(d.getMinutes()).padStart(2, '0');
  
  return `${year}-${month}-${day} ${hours}:${minutes}`;
}

// 隐藏敏感信息
function hideSensitiveInfo(str, start = 3, end = 4) {
  if (!str) return '';
  if (str.length <= start + end) {
    return str.charAt(0) + '****' + str.charAt(str.length - 1);
  }
  return str.substring(0, start) + '****' + str.substring(str.length - end);
}

// 验证手机号
function validatePhone(phone) {
  const reg = /^1[3-9]\d{9}$/;
  return reg.test(phone);
}

// 验证邮箱
function validateEmail(email) {
  const reg = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/;
  return reg.test(email);
}

// 验证QQ号
function validateQQ(qq) {
  const reg = /^[1-9]\d{4,10}$/;
  return reg.test(qq);
}

// 验证微信号
function validateWechat(wechat) {
  const reg = /^[a-zA-Z][a-zA-Z0-9_-]{5,19}$/;
  return reg.test(wechat);
}

module.exports = {
  formatTime,
  formatDate,
  hideSensitiveInfo,
  validatePhone,
  validateEmail,
  validateQQ,
  validateWechat
};